package com.ivncorp.evnproject;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.ivncorp.evnproject.api.RestAPI;

public class ActivityProfile extends Activity {
	
	private EditText txtFullName;
	private EditText txtEmail;
	private Button btnCancel;
	private Button btnSave;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_profile);
		getActionBar().setDisplayHomeAsUpEnabled(true);
		
		txtFullName = (EditText) findViewById(R.id.txtProfileFirstName);
		txtEmail = (EditText) findViewById(R.id.txtProfileLastName);
		btnCancel = (Button) findViewById(R.id.btnProfileCancel);
		btnSave = (Button) findViewById(R.id.btnProfileSave);
		
		btnCancel.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ActivityProfile.this.finish();
			}
		});
		
		btnSave.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
//				RestAPI api =  new RestAPI();
//				api.
				
			}
		});

		new AsyncProfile().execute("admin");
	}
	
	private ProgressDialog waitingDialog = null;
	
	private class AsyncProfile extends AsyncTask<String, Void, JSONObject>{
		
		@Override
		protected void onPreExecute() {
			
			waitingDialog = ProgressDialog.show(ActivityProfile.this, "Loading...", "");
			waitingDialog.setCanceledOnTouchOutside(false);
			waitingDialog.setCancelable(false);
			waitingDialog.show();
			
			super.onPreExecute();
		}

		@Override
		protected JSONObject doInBackground(String... params) {
			RestAPI api = new RestAPI();
			JSONObject result = null;
			
			try {
				result = api.GetUser(params[0]);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			return result;//{"Value":[{"firstName":"admin","lastName":"admin"}],"Successful":true}
		}
		
		@Override
		protected void onPostExecute(JSONObject result) {
			
			if (waitingDialog.isShowing() == true) {
				waitingDialog.dismiss();
			}
			
			try {
				if (result.getString("Successful").equals("true")){
					JSONObject object = (result.getJSONArray("Value")).getJSONObject(0);
					String Fullname = object.getString("Fullname");
					if (!Fullname.isEmpty()){
						txtFullName.setText(Fullname);
					}
					String Email = object.getString("Email");
					if (!Email.isEmpty()){
						txtEmail.setText(Email);
					}
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			super.onPostExecute(result);
		}
	}
	
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		// TODO Auto-generated method stub
		switch(item.getItemId()){
		case android.R.id.home:
			finish();
			break;
		default:
			break;
		}
		return super.onMenuItemSelected(featureId, item);
	}

}
